package com.atmaowei.service.impl;

import com.atmaowei.controller.request.BaseRequest;
import com.atmaowei.entity.Department;
import com.atmaowei.entity.Student;
import com.atmaowei.mapper.StudentMapper;
import com.atmaowei.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void save(Student student) {
        studentMapper.save(student);
    }

    @Override
    public PageInfo<Student> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
        List<Student> list = studentMapper.page(baseRequest);
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void delete(String sno) {
        studentMapper.delete(sno);
    }

    @Override
    public void update(Student student, String oldSno) {
        studentMapper.update(student, oldSno);
    }

    @Override
    public Student getBySno(String sno) {
        return studentMapper.getBySno(sno);
    }

    @Override
    public List<Department> getAllDept() {
        return studentMapper.getAllDept();
    }

    @Override
    public void deleteSelected(List<Student> students) {
        for (Student student : students) {
            studentMapper.deleteSelected(student.getSno());
        }
    }

    @Override
    public void statusChange(String sno) {
        Student student = studentMapper.getBySno(sno);
        String status = null;
        if (student.getStatus().equals("1")) {
            status = "0";
            studentMapper.statusChange(sno,status);
        } else {
            status = "1";
            studentMapper.statusChange(sno,status);
        }
    }
}
